import queue
import tkinter as tk


class UIClass():
    def __init__(self, root):
        self.root = root  # 主窗口
        self.labels = []
        self.data_queue = queue.Queue()

    def initWindow(self, title, geometry):
        self.root.title(title)
        self.root.geometry(geometry)

    def update(self):
        while not self.data_queue.empty():  # 数据队列不空
            data = self.data_queue.get()  # 获取数据
            for l in self.labels:
                if l['id'] == data[0]:
                    #print('匹配')
                    label = l['label']
                    func = l['function']
                    func(label, data)
        self.root.after(100, self.update)

    def loop(self):
        self.update()
        self.root.mainloop()

    def addLabel(self, text, color, font, width, height, id, updataFun, row, column):
        self.label = tk.Label(self.root, text=text, bg=color, font=font, width=width, height=height)
        self.label.place(x=row, y=column)
        self.labels.append({'label': self.label, 'id': id, 'function': updataFun})  # 绑定标签和id，用于更新数据
